<?php

/**
 * Module registrasi Step1
 * @author andro
 * @date 4-juli-2011
 */
class registrasi_form extends CFormModel {

    public $member_address;
    public $member_upline;
    public $member_sponsor;
    public $member_zipcode;
    public $member_state_id;
    public $member_city_id;
    public $sponsor_mid;
    public $member_name;
    public $member_nickname;
    public $member_email;
    public $member_birth_date;
    public $member_birth_place;
    public $serial_id;
    public $serial_pin;
    public $position;
    public $password;
    public $password2;
    public $member_sex;
    public $member_identity_type;
    public $member_identity_no;
    public $member_phone;
    public $member_mobilephone;
    public $fix;
    public $member_bank_branch;
    public $member_bank_id;
    public $member_bank_city;
    public $member_bank_account_name;
    public $member_bank_account_no;
    public $member_devisor_name;
    public $member_devisor_relationship;
    public $member_devisor_phone;

    public function rules() {
//        return array(
//            array('upline_mid, member_sex, member_name, sponsor_mid, position, member_sex, serial_id, serial_pin, member_nickname, member_mobilephone', 'required'),
//            array('serial_id', 'cek_serial'),
//            array('password', 'length', 'min' => '4'),
//            array('password2', 'compare', 'compareAttribute' => 'password'),
//            array('fix', 'required', 'requiredValue' => 'active')
//        );
        //buat registrasi langsung
        return array(
            array('member_sponsor, member_upline, position, position,serial_id, serial_pin, member_nickname', 'required', 'on' => 'registrasi'),
            array('fix' , 'required', 'requiredValue' => 'active' , 'on' => 'registrasi'),
            array('member_nickname', 'cek_username', 'on' => 'registrasi' ),
            array('serial_id', 'cek_serial', 'on' => 'registrasi'),
            array('member_sponsor, member_upline', 'cek_member', ),
            array('member_sponsor, member_upline', 'cek_crossline', ),
            array('member_sponsor','cek_active'),
            array('member_name, member_mobilephone', 'required'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'member_sponsor' => 'Sponsor ID',
            'member_upline' => 'Upline ID',
            'password2' => 'Confirm Password',
            'serial_id' => 'Serial Number',
            'serial_pin' => 'PIN',
            'fix' => 'Term of Service Agreement',
            'member_name' => 'Fullname',
            'member_nickname' => 'Nickname',
            'member_sex' => 'Jenis Kelamin',
            'member_mobilephone' => 'Mobilephone',
        );
    }

    /**
     * Callback Validasi untuk pengecekan validasi
     * @param $attribute
     * @param $params
     */
    public function cek_serial($attribute, $params) {
        $serial_id = $this->serial_id;
        $serial_pin = $this->serial_pin;
        //ambil serial berdasar IDnya
        $serial = sys_serial::model()->find("serial_id=:serial_id", array(':serial_id' => $this->serial_id));
        if (empty($serial)) {
            $message = Yii::t('yii', 'Serial Tidak Terdapat Dalam Database Kami.');
            $this->addError($attribute, $message);
        } elseif ($serial->serial_is_active == '0') {
            $message = Yii::t('yii', 'Serial ' . $this->serial_id . ' Belum Aktif');
            $this->addError($attribute, $message);
        } elseif ($serial->serial_is_used == '1') {
            $message = Yii::t('yii', 'Serial ' . $this->serial_id . ' Telah Dipakai');
            $this->addError($attribute, $message);
        } elseif ($serial->serial_pin !== $this->serial_pin) {
            $message = Yii::t('yii', 'Serial PIN Yang Anda Masukkan Salah');
            $this->addError($attribute, $message);
        }
    }
    /**
     * Cek crossline
     */
    public function cek_crossline($attribute, $params) {
        $sponsor = $this->member_sponsor;
        $upline = $this->member_upline;
        $return_value = mlm_function::check_uplink($upline, $sponsor);
        if($return_value == "F") {
            $message = Yii::t('yii', ' Sponsor : \'<strong>'.$sponsor.'</strong>\' tidak sejalur dengan upline : '.$upline);
            $this->addError($attribute, $message);
        } 
    }
    /**
     * Callback buat ngecek member ada atau tidak
     */
    public function cek_member($attribute, $params) {
        $network_mid = $this->$attribute;//dimasukkan ke variable biar lebih dinamis($this->$attribute)
        $network_id = function_lib::get_one('network_id', 'sys_network', 'network_mid = \''.$network_mid.'\'');
        if($network_id == "" || $network_id == '0') {
            $message = Yii::t('yii', 'Member dengan ID \'<strong>'.$this->$attribute.'</strong>\' tidak ada dalam database');
            $this->addError($attribute, $message);
        }
    }
    /**
     * Callback buat ngecek username tidak boleh menggunakan karakter yang tidak
     */
    public function cek_username($attribute, $params) {
        $member_nickname = strtolower($this->member_nickname);
        $cek = function_lib::get_one('member_network_id', 'sys_member', 'member_nickname = \''.$member_nickname.'\'');
        preg_match('/^[a-zA-Z0-9_-]*$/', $member_nickname, $match);
        if(empty($match)) {
            $message = Yii::t('yii', 'Username / Nama Alias Mengandung Kata Ilegal');
            $this->addError($attribute, $message);
        }
        if($cek != "") {
            $message = Yii::t('yii', 'Username / Nama Alias \'<strong>'.$this->member_nickname.'</strong>\' sudah digunakan');
            $this->addError($attribute, $message);
        }
    }
    public function cek_active($attribute, $params) {
        $member_sponsor = $this->member_sponsor;
        $cek = function_lib::get_one('network_level_status', 'sys_network', 'network_mid = \''.$member_sponsor.'\'');
        if($cek == 'passive' || $cek == '') {
            $message = Yii::t('yii', 'Member \'<strong>'.$this->member_sponsor.'</strong>\' Belum Aktif');
            $this->addError($attribute, $message);
        }
    }
}

?>
